package com.xuxin.baby.fragment;

import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.xuxin.baby.R;
import com.xuxin.baby.base.BaseConstant;
import com.xuxin.baby.base.BaseFragment;
import com.xuxin.baby.bean.BaseBean;
import com.xuxin.baby.contract.ChildEatingRecordContract;
import com.xuxin.baby.event.BaseEventFragment;
import com.xuxin.baby.event.EatingDriedEvent;
import com.xuxin.baby.presenter.ChildEatingRecordPresenter;
import com.xuxin.baby.utils.ACache;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;

/**
 * Created by xu_xin
 * Created on 2020.08.17 0017
 * 记喂养 -> 奶粉
 */
public class ChildEatingDriedFragment extends BaseEventFragment implements ChildEatingRecordContract.View {
    private final ChildEatingRecordContract.Presenter presenter = new ChildEatingRecordPresenter(this);
    @BindView(R.id.child_eating_dried_ml)
    EditText child_eating_dried_ml;//奶粉喂养量ml

    private String tenantId;//租户id
    private String token;//授权token
    private String childId;//宝宝ID
    private float feedPowderedAmt;//奶粉喂养量ml

    public static ChildEatingDriedFragment newInstance() {
        return new ChildEatingDriedFragment();
    }

    @Override
    public View setContentView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container) {
        return inflater.inflate(R.layout.fragment_child_eating_dried, container, false);
    }

    @Override
    public void initView() {
    }

    @Override
    public void initData() {
        ACache aCache = ACache.get(getContext());
        tenantId = aCache.getAsString(BaseConstant.tenantId);
        token = aCache.getAsString(BaseConstant.token);
        childId = aCache.getAsString(BaseConstant.childId);
    }

    @Override
    public void onComplete() {
        dismiss();
    }

    @Override
    public void onClick(View view) {

    }

    private final Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            if (msg.what == 0) {//瓶装母乳喂养量ml
                presenter.recordDried(tenantId, token, childId, TimeUtils.getNowString(), feedPowderedAmt);
            }
            return false;
        }
    });

    @Override
    public void onSuccess(BaseBean baseBean) {
        requireActivity().finish();
        ToastUtils.showShort("保存成功");
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(EatingDriedEvent event) {
        try {
            String ml = child_eating_dried_ml.getText().toString();
            if (!StringUtils.isEmpty(ml)) {
                LogUtils.e("记喂养 -> 奶粉 " );
               feedPowderedAmt = Float.parseFloat(ml);
                popup.show();
                handler.sendEmptyMessage(0);
            } else {
                ToastUtils.showShort("请输入喂养量");
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
            dismiss();
        }
    }
}